package com.may;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;

import java.io.InputStream;
import java.util.zip.ZipInputStream;

/**
 * 流程部署（定义）
 *
 * 影响的表：
 * act_re_deployment 记录部署信息
 * act_re_procdef 其中的 key_ 是用来区分不同工作流
 * act_ge_bytearray 保存了发布时的资源 .bpmn .png
 */
public class ActivitiDeployment {
    public static void main(String[] args) {
        //部署方式，两者选其一即可
//        deployment();
            deploymentByZip();
    }

    //方式一，常规部署
    private static void deployment(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("bpmn\\mybpm.bpmn")
                .addClasspathResource("bpmn\\mybpm.png")
                .name("请假流程")
                .deploy();
        System.out.println(deploy.getName());
        System.out.println(deploy.getId());
    }

    //方式二，压缩包部署。部署之后会自动解压在数据库中
    private static void deploymentByZip(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = engine.getRepositoryService();

        InputStream inputStream = ActivitiDeployment.class.getClassLoader().getResourceAsStream("bpmn\\myBPMN.zip");
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);

        Deployment deploy = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name("请假流程")
                .deploy();
        System.out.println(deploy.getName());
        System.out.println(deploy.getId());
    }
}
